package com.adv.ems.manager.impl;

import com.adv.ems.constants.EmsConst;
import com.adv.ems.db.mapper.CircuitDiagramNodeMapper;
import com.adv.ems.db.entity.CircuitDiagramNodeDO;
import com.adv.ems.manager.CircuitDiagramNodeManager;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CircuitDiagramNodeManagerImpl extends BaseManagerImpl<CircuitDiagramNodeMapper, CircuitDiagramNodeDO> implements CircuitDiagramNodeManager {
    @Override
    public void deleteByPath(Long id) {
        List<Long> ids = getAllMap()
                .values()
                .stream()
                .filter(d -> {
                    if (d.getId().equals(id)) {
                        return true;
                    }
                    String pathNode = String.format(EmsConst.PATH_FORMAT, id);
                    if (d.getPath() != null && d.getPath().contains(pathNode)) {
                        return true;
                    }
                    return false;
                }).map(CircuitDiagramNodeDO::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(ids)) {
            removeBatchByIds(ids);
        }
    }
}
